#include "head.h"

// 实现用户登录功能，因为是利用linux下的用户，因此要在root下执行
int loginFunc(int sockfd) {
    // 初始化变量
    int password_tries_left = 4;// 记录用户输入密码的剩余尝试次数，初始值为4
    int state_is; // 用于存储从服务器接收到的登录状态码
    train_t train_name; // 存储用户名
    train_t train_password; // 存储用户名密码

    // 第一次登录
    // 清空结构体
    bzero(&train_name, sizeof(train_name));
    bzero(&train_password, sizeof(train_password));
    
    // 提示用户输入用户名和密码
    printf("Please enter your username: ");
    
    fgets(train_name.data, sizeof(train_name.data), stdin);
    train_name.data[strlen(train_name.data) - 1] = '\0';
    train_name.length = strlen(train_name.data);

    printf("Please enter your password: ");
    fgets(train_password.data, sizeof(train_password.data), stdin);
    train_password.data[strlen(train_password.data) - 1] = '\0';
    train_password.length = strlen(train_password.data);
    
    // MSG_NOSIGNAL 标志用于防止发送过程中产生 SIGPIPE 信号
    // 将用户名的长度、用户名内容和密码的长度和密码内容分别发送给服务器
    send(sockfd, &train_name, sizeof(train_name.length)+train_name.length, MSG_NOSIGNAL);
    send(sockfd, &train_password, sizeof(train_password.length)+train_password.length, MSG_NOSIGNAL);

    // 根据服务器返回的状态码处理登录结果
    while (1) {
        // 接收一个int
        recv(sockfd, &state_is,sizeof(int), 0);

        if (state_is == 0) return 0;
        else if (state_is == 1) {
            // 密码错误
            password_tries_left--;
            if (password_tries_left == 0) {
                // 密码尝试次数耗尽，返回-1
                return -1;
            }

            printf(" Password incorrect. Attempts remaining: [%d].\n", password_tries_left);
            printf("Please enter your password: ");

            bzero(&train_password, sizeof(train_password));
            fgets(train_password.data, sizeof(train_password.data), stdin);
            train_password.data[strlen(train_password.data) - 1] = '\0';
            train_password.length = strlen(train_password.data);
            send(sockfd, &train_name, sizeof(train_name.length)+train_name.length, MSG_NOSIGNAL);
            send(sockfd, &train_password, sizeof(train_password.length)+train_password.length, MSG_NOSIGNAL);
        } else if (state_is == 2) {
            // 不存在该用户
            printf("The username you entered does not exist.\n");
            bzero(&train_name, sizeof(train_name));
            bzero(&train_password, sizeof(train_password));
            printf("Please enter your username: ");
            fgets(train_name.data, sizeof(train_name.data), stdin);
            train_name.data[strlen(train_name.data) - 1] = '\0';
            train_name.length = strlen(train_name.data);

            printf("Please enter your password: ");
            fgets(train_password.data, sizeof(train_password.data), stdin);
            train_password.data[strlen(train_password.data) - 1] = '\0';
            train_password.length = strlen(train_password.data);
            send(sockfd, &train_name, sizeof(train_name.length)+train_name.length, MSG_NOSIGNAL);
            send(sockfd, &train_password, sizeof(train_password.length)+train_password.length, MSG_NOSIGNAL);


        }
    }
    return 0;
}

